package cn.edu.swpu.cins.u_chain.frontend.service.user.Impl;

import cn.edu.swpu.cins.u_chain.Exception.user.CompanyException;
import cn.edu.swpu.cins.u_chain.Exception.user.UserException;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.CompanyDao;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.UserDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.UpdateRole;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.company.CompanyDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.company.CompanyInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.BasicInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.LocationInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.company.CompanyInfoDB;
import cn.edu.swpu.cins.u_chain.frontend.enums.UserRole;
import cn.edu.swpu.cins.u_chain.frontend.service.user.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class,CompanyException.class,SQLException.class})
    public int addInfo(CompanyInfo companyInfo) {
        CompanyInfoDB companyInfoDB = new CompanyInfoDB(companyInfo);
        companyDao.addCompanyInfo(companyInfoDB);
        return userDao.updateRole(new UpdateRole(companyInfo.getUserId(), UserRole.COMPANY.getRole()));

    }

    @Override
    public CompanyDetail selectComDetail(int userId) {
        return companyDao.selectCompanyDetail(userId);
    }


}
